﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
using System.Management;
using System.Drawing.Printing;
namespace BYManageInfoSystem
{
    public partial class Frm_GDTYInvoice : DockContent
    {
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        public static DGBY.BLL.TaoDaPrinterNameTab bllTaoDaPrinterNameTab = new DGBY.BLL.TaoDaPrinterNameTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
    //    private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        private DGBY.BLL.MaoYiFSTab m_bllMaoYiFSTab = new DGBY.BLL.MaoYiFSTab();
        private DGBY.BLL.JieHuiFSTab m_bllJieHuiFSTab = new DGBY.BLL.JieHuiFSTab();
        private DGBY.BLL.YSMethodTab m_bllYSMethodTab = new DGBY.BLL.YSMethodTab();
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
 
        private DGBY.BLL.ZhuanYPortTab m_bllZhuanYPortTab = new DGBY.BLL.ZhuanYPortTab();
        private DGBY.BLL.ZYHarborTab m_bllZYHarborTab = new DGBY.BLL.ZYHarborTab();
        private DGBY.BLL.BYCompBankCountTab m_bllBYCompBankCountTab = new DGBY.BLL.BYCompBankCountTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.LogRecTab m_bllLogRecTab = new DGBY.BLL.LogRecTab();

        private DGBY.BLL.SHCompTab m_bllSHCompTab = new DGBY.BLL.SHCompTab();
        private DGBY.BLL.TYInvoiceTab m_bllTYInvoiceTab = new DGBY.BLL.TYInvoiceTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private int m_nSelectTYInvoiceTabID = 0;
       
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        public Frm_GDTYInvoice()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllPaidanRegisterTab.GetNameAndID("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }
            ds = m_bllYSMethodTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTransWay.DataSource = ds.Tables[0];
                this.comboBoxTransWay.DisplayMember = "运输方式";
                this.comboBoxTransWay.ValueMember = "YSMethodTabID";
                this.comboBoxTransWay.SelectedIndex = 0;
            }


            ds = m_bllMDCountryTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTargetPort.DataSource = ds.Tables[0];
                this.comboBoxTargetPort.DisplayMember = "国家名称";
                this.comboBoxTargetPort.ValueMember = "MDCountryTabID";
                this.comboBoxTargetPort.SelectedIndex = 0;
            }
            
            ds = m_bllMaoYiFSTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxMYiWay.DataSource = ds.Tables[0];
                this.comboBoxMYiWay.DisplayMember = "贸易方式";
                this.comboBoxMYiWay.ValueMember = "MaoYiFSTabID";
                this.comboBoxMYiWay.SelectedIndex = 0;
            }

            ds = m_bllJieHuiFSTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxJHuiWay.DataSource = ds.Tables[0];
                this.comboBoxJHuiWay.DisplayMember = "结汇方式";
                this.comboBoxJHuiWay.ValueMember = "JieHuiFSTabID";
                this.comboBoxJHuiWay.SelectedIndex = 0;
            }

            ds = m_bllZYHarborTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxSendPort.DataSource = ds.Tables[0];
                this.comboBoxSendPort.DisplayMember = "装运港名称";
                this.comboBoxSendPort.ValueMember = "ZYHarborTabID";
                this.comboBoxSendPort.SelectedIndex = 0;
            }

            ds = m_bllBYCompBankCountTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBank.DataSource = ds.Tables[0];
                this.comboBoxBank.DisplayMember = "开户行";
                this.comboBoxBank.ValueMember = "BYCompBankCountTabID";
                this.comboBoxBank.SelectedIndex = 0;
            }
          
        }

        private void Frm_GDTYInvoice_Load(object sender, EventArgs e)
        {
            //this.groupBoxGoodsDetail.Visible = false;
            InitCombox();
            
         
        }

        private void ShowAllMXRecordInDataGridView(string strContract)
        {

            ultraGrid2.DataSource = null;
            DataSet m_ds = m_bllTYInvoiceTab.ShowTYInvoiceMXRec(strContract);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {

                //Decimal nNumTotal = 0;

                //Decimal dtSingleJinE = 0;
                //Decimal dtTotalJinE = 0;
                //for (int i = 0; i < m_ds.Tables[0].Rows.Count; i++)
                //{

                //    nNumTotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["数量"].ToString());
                //    dtSingleJinE += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["销售单价"].ToString());
                //    dtTotalJinE += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["销售总额"].ToString());
                //}

                //DataRow dr = m_ds.Tables[0].NewRow();

                //dr["数量"] = nNumTotal;

                //dr["销售单价"] = dtSingleJinE;
                //dr["销售总额"] = dtTotalJinE;
                //dr["商品名称"] = "合计";
                //m_ds.Tables[0].Rows.Add(dr.ItemArray);
                m_ds.Tables[0].Columns["打折单价"].ColumnName = "报关单价";
                m_ds.Tables[0].Columns["打折金额"].ColumnName = "报关金额";
                ultraGrid2.DataSource = m_ds.Tables[0];
                if (Frm_Main.m_bCompleteVer == false)
                {
                    ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Hidden = true;
                    ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Hidden = true;
                }
                //dataGridViewGoodsDetailList.Columns[0].Visible = false;
            }
        }

        private void ShowInfoByContractNo(string strContractNo)
        {
            int nID = Convert.ToInt32(strContractNo, 10);
            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("TYInvoiceTab.PaidanRegisterTabID = '" + nID + "'");


            ultraGrid1.DataSource = null;
            ultraGrid2.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    ultraGrid1.DataSource = ds.Tables[0];
            //    ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            //    ShowAllMXRecordInDataGridView(nID);
            //}
            //else
            {

                m_nSelectTYInvoiceTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["TYInvoiceTabID"].ToString(), 10);
                cellTYInvoiceTabClick();
                ultraGrid1.DataSource = null;
                ultraGrid2.DataSource = null;
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                //ShowAllMXRecordInDataGridView(strContractNo);
                //ds = m_bllBYCompanyTab.GetJYCompNameEtc("PaidanRegisterTabID = '" + nID + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    this.textBoxSellComp.Text = ds.Tables[0].Rows[0]["销货单位"].ToString();
                //    this.textBoxSellCompAddr.Text = ds.Tables[0].Rows[0]["销货单位地址"].ToString();
                //    this.textBoxSellCompTel.Text = ds.Tables[0].Rows[0]["销货单位电话"].ToString();
                //    this.textBoxSellCompFax.Text = ds.Tables[0].Rows[0]["销货单位传真"].ToString();
                //}
                //ds = m_bllSHCompTab.GetSHCompNameEtc("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    this.textBoxBuySide.Text = ds.Tables[0].Rows[0]["购货单位"].ToString();
                //    this.textBoxBuySideAddr.Text = ds.Tables[0].Rows[0]["购货单位地址"].ToString();
                //    this.textBoxBuySideTel.Text = ds.Tables[0].Rows[0]["购货单位电话"].ToString();
                //    this.comboBoxSendPort.SelectedValue = ds.Tables[0].Rows[0]["装运港ID"].ToString();
                //}
                //ds = m_bllBYCompBankCountTab.GetAccountInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    this.comboBoxBank.DataSource = ds.Tables[0];
                //    this.comboBoxBank.DisplayMember = "开户行";
                //    this.comboBoxBank.ValueMember = "BYCompBankCountTabID";
                //    this.comboBoxBank.SelectedIndex = 0;
                //}
                ////                strSql.Append(@"select BGCKHTTabID,BGCKHTDJDate,BGCKHTDJNo
                ////,PaidanRegisterTabID,JYCompDaiBiao,SHCompDaiBiao,QDPlaceTabID,JNHYPosTabID,YSMethodTabID
                ////,MDCountryTabID,ZYHarborTabID,SHCompTabID,BZSortTabID,QDDate,JHDate,DYZ,SCPiNo,BaoXian
                ////,FKCondition,SPCheck,ZYMethod,QTThing,Note,InvoiceNo,InvoiceNote ");

                //ds = m_bllBGCKHTTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    this.comboBoxTransWay.SelectedValue = ds.Tables[0].Rows[0]["YSMethodTabID"].ToString();
                //    this.comboBoxTargetPort.SelectedValue = ds.Tables[0].Rows[0]["MDPortTabID"].ToString();
                //    this.comboBoxMYiWay.SelectedValue = ds.Tables[0].Rows[0]["MaoYiFSTabID"].ToString();
                //    this.comboBoxJHuiWay.SelectedValue = ds.Tables[0].Rows[0]["JieHuiFSTabID"].ToString();
                //}

                cellTYInvoiceTabClick();
            }
            else
            {
                m_nSelectTYInvoiceTabID = 0;
            }


        }

        private void ResetControl()
        {
            this.textBoxSellComp.Text = "";
            this.textBoxSellCompAddr.Text = "";
            this.textBoxSellCompTel.Text = "";
            this.textBoxSellCompFax.Text = "";
            this.textBoxBuySide.Text = "";
            this.textBoxBuySideAddr.Text = "";
            this.textBoxBuySideTel.Text = "";
            this.textBoxNote.Text = "";
            this.textBoxLCNo.Text = "";
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                //ResetControl();
                ShowInfoByContractNo(str);

            }
            catch { }
        }

        private void comboBoxBank_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec(strWhere);

            ultraGrid1.DataSource = null;
            //ultraGrid2.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            DataSet m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //if (m_nPaidanRegisterTabID != 0)
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                //else
                // this.comboBoxContractNo.SelectedIndex = Frm_Main.m_nContractSelectIndex;
            }
            this.ultraGrid1.Dock = DockStyle.Fill;
            this.groupBoxGoodsDetail.Visible = false;
            ShowAllRecord("");

        }

        private void buttonAddBGDComebackSheet_Click(object sender, EventArgs e)
        {
            try
            {

                DGBY.Model.TYInvoiceTab modelTYInvoiceTab = new DGBY.Model.TYInvoiceTab();

                int nID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);
                DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");


                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("该合同号的记录已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //ds = m_bllBGDComeBackTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    modelTYInvoiceTab.BGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGDComeBackTabID"].ToString(), 10);
                //}
                //else
                //{
                //    MessageBox.Show("回收报关单不存在该合同号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
                modelTYInvoiceTab.PaidanRegisterTabID = nID;
                modelTYInvoiceTab.Note = this.textBoxNote.Text;
                modelTYInvoiceTab.LCNo = this.textBoxLCNo.Text;
                modelTYInvoiceTab.IsPrint = 0;
                modelTYInvoiceTab.BYCompBankCountTabID = Convert.ToInt32(this.comboBoxBank.SelectedValue.ToString(), 10);
                modelTYInvoiceTab.MaoYiFSTabID = Convert.ToInt32(this.comboBoxMYiWay.SelectedValue.ToString(), 10);
                modelTYInvoiceTab.JieHuiFSTabID = Convert.ToInt32(this.comboBoxJHuiWay.SelectedValue.ToString(), 10);
           
                modelTYInvoiceTab.KPDate = this.dateTimePickerMakeDateFake.Value.Date;

                modelTYInvoiceTab.ZhuanYPort = this.textBoxZhuanYPort.Text;

                modelTYInvoiceTab.PrintDate = DateTime.Parse("1900-01-01");
                modelTYInvoiceTab.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);
                m_bllTYInvoiceTab.Add(modelTYInvoiceTab);

                Frm_Main.Log2DB(this.Text, "增加", this.comboBoxContractNo.Text);
                ShowAllRecord("PaidanRegisterTab.PaidanRegisterTabID = '" + nID + "'");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }





        //private void dataGridViewInvoiceList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    string strTYInvoiceTabID = this.dataGridViewInvoiceList.Rows[this.dataGridViewInvoiceList.CurrentCell.RowIndex].Cells[0].Value.ToString();
        //    m_nSelectTYInvoiceTabID = Convert.ToInt32(strTYInvoiceTabID, 10);
        //    cellTYInvoiceTabClick();
        //}

        private void cellTYInvoiceTabClick()
        {
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            DGBY.Model.TYInvoiceTab modelTYInvoiceTab = m_bllTYInvoiceTab.GetModel(m_nSelectTYInvoiceTabID);
            if (modelTYInvoiceTab != null)
            {
                //DataSet ds = m_bllBGDComeBackTab.GetList("BGDComeBackTabID = '" + modelTYInvoiceTab.BGDComeBackTabID + "'");
                //int nID = 0;
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    DataSet ds = m_bllBYCompanyTab.GetJYCompNameEtc("PaidanRegisterTabID = '" + modelTYInvoiceTab.PaidanRegisterTabID + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        this.textBoxSellComp.Text = ds.Tables[0].Rows[0]["销货单位"].ToString();
                        this.textBoxSellCompAddr.Text = ds.Tables[0].Rows[0]["销货单位地址"].ToString();
                        this.textBoxSellCompTel.Text = ds.Tables[0].Rows[0]["销货单位电话"].ToString();
                        this.textBoxSellCompFax.Text = ds.Tables[0].Rows[0]["销货单位传真"].ToString();
                    }
                    ds = m_bllSHCompTab.GetSHCompNameEtc("PaidanRegisterTab.PaidanRegisterTabID = '" + modelTYInvoiceTab.PaidanRegisterTabID + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        this.textBoxBuySide.Text = ds.Tables[0].Rows[0]["购货单位"].ToString();
                        this.textBoxBuySideAddr.Text = ds.Tables[0].Rows[0]["购货单位地址"].ToString();
                        this.textBoxBuySideTel.Text = ds.Tables[0].Rows[0]["购货单位电话"].ToString();
                        this.comboBoxSendPort.SelectedValue = ds.Tables[0].Rows[0]["装运港ID"].ToString();

                    }
                    //                strSql.Append(@"select BGCKHTTabID,BGCKHTDJDate,BGCKHTDJNo
                    //,PaidanRegisterTabID,JYCompDaiBiao,SHCompDaiBiao,QDPlaceTabID,JNHYPosTabID,YSMethodTabID
                    //,MDCountryTabID,ZYHarborTabID,SHCompTabID,BZSortTabID,QDDate,JHDate,DYZ,SCPiNo,BaoXian
                    //,FKCondition,SPCheck,ZYMethod,QTThing,Note,InvoiceNo,InvoiceNote ");
                    /*
                    ds = m_bllBGCKHTTab.GetList("PaidanRegisterTabID = '" + modelTYInvoiceTab.PaidanRegisterTabID + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        this.comboBoxTransWay.SelectedValue = ds.Tables[0].Rows[0]["YSMethodTabID"].ToString();
                        this.comboBoxTargetPort.SelectedValue = ds.Tables[0].Rows[0]["MDCountryTabID"].ToString();
                      //  this.comboBoxSendPort.SelectedValue = ds.Tables[0].Rows[0]["ZYHarborTabID"].ToString();
           
                    }
                    */

                    this.comboBoxTransWay.SelectedValue = modelTYInvoiceTab.YSMethodTabID;

                    this.comboBoxMYiWay.SelectedValue = modelTYInvoiceTab.MaoYiFSTabID;
                    this.comboBoxJHuiWay.SelectedValue = modelTYInvoiceTab.JieHuiFSTabID;
                    
                    this.comboBoxBank.SelectedValue = modelTYInvoiceTab.BYCompBankCountTabID;
                    this.textBoxZhuanYPort.Text = modelTYInvoiceTab.ZhuanYPort;
                    this.comboBoxContractNo.SelectedValue = modelTYInvoiceTab.PaidanRegisterTabID;
                 //   this.dateTimePickerMakeDateFake.Value = (System.DateTime)modelTYInvoiceTab.KPDate;

                    //this.textBoxNote.Text = modelTYInvoiceTab.Note;
                    this.textBoxLCNo.Text = modelTYInvoiceTab.LCNo;
                    ShowAllMXRecordInDataGridView(this.comboBoxContractNo.Text);
                }
               
            }

        }

        private void buttonModifyBGDComebackSheet_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.TYInvoiceTab modelTYInvoiceTabOld = m_bllTYInvoiceTab.GetModel(m_nSelectTYInvoiceTabID);
                if (modelTYInvoiceTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

              //  DGBY.Model.TYInvoiceTab modelTYInvoiceTab = new DGBY.Model.TYInvoiceTab();
             //   modelTYInvoiceTab.TYInvoiceTabID = m_nSelectTYInvoiceTabID;

                modelTYInvoiceTabOld.Note = this.textBoxNote.Text;
                modelTYInvoiceTabOld.LCNo = this.textBoxLCNo.Text;
                //modelTYInvoiceTabOld.PaidanRegisterTabID = modelTYInvoiceTabOld.PaidanRegisterTabID;
                //modelTYInvoiceTabOld.IsPrint = modelTYInvoiceTabOld.IsPrint;
                //modelTYInvoiceTab.IsPrint = modelTYInvoiceTabOld.IsPrint;
                modelTYInvoiceTabOld.BYCompBankCountTabID = Convert.ToInt32(this.comboBoxBank.SelectedValue.ToString(), 10);
                modelTYInvoiceTabOld.MaoYiFSTabID = Convert.ToInt32(this.comboBoxMYiWay.SelectedValue.ToString(), 10);
                modelTYInvoiceTabOld.JieHuiFSTabID = Convert.ToInt32(this.comboBoxJHuiWay.SelectedValue.ToString(), 10);

                //修改运输方式
                modelTYInvoiceTabOld.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);

                modelTYInvoiceTabOld.KPDate = this.dateTimePickerMakeDateFake.Value.Date;

                modelTYInvoiceTabOld.ZhuanYPort = this.textBoxZhuanYPort.Text;

                m_bllTYInvoiceTab.Update(modelTYInvoiceTabOld);
                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxContractNo.Text);
                ShowAllRecord("PaidanRegisterTab.PaidanRegisterTabID = '" + modelTYInvoiceTabOld.PaidanRegisterTabID + "'");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelBGDComebackSheet_Click(object sender, EventArgs e)
        {
            DGBY.Model.TYInvoiceTab modelTYInvoiceTabOld = m_bllTYInvoiceTab.GetModel(m_nSelectTYInvoiceTabID);
            if (modelTYInvoiceTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DGBY.Model.PaidanRegisterTab modelPaidanRegisterTabOld = m_bllPaidanRegisterTab.GetModel(modelTYInvoiceTabOld.PaidanRegisterTabID);


 
            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllTYInvoiceTab.Delete(m_nSelectTYInvoiceTabID);
                DGBY.Model.LogRecTab modelLogRecTab = new DGBY.Model.LogRecTab();
                modelLogRecTab.LogDate = System.DateTime.Now.Date;
                modelLogRecTab.LogContent = Frm_Main.m_strLoginUser + "删除了合同号"
                    + modelPaidanRegisterTabOld.ContractCode + "的广东省出口统一发票";
                m_bllLogRecTab.Add(modelLogRecTab);
                Frm_Main.Log2DB(this.Text, "删除", modelPaidanRegisterTabOld.ContractCode);
                ShowAllRecord("");
                ultraGrid2.DataSource = null;
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxNote.Text = "";
                this.textBoxLCNo.Text = "";

                return;
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = null;
            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet m_ds = m_bllTYInvoiceTab.ShowTYInvoiceMXRec(this.comboBoxContractNo.Text);
            string strBiZhiEng = "";
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = (DataTable)m_ds.Tables[0];
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("销售单价");
                    dt.Columns.Remove("销售总额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
   
                string strBiZhi = m_ds.Tables[0].Rows[0]["币种"].ToString();
                DataSet dsTemp = m_bllBiZhiTab.GetList("BiZhiTab.BiZhiName = '" + strBiZhi + "'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    strBiZhiEng = dsTemp.Tables[0].Rows[0]["币制简写"].ToString();
                }
            }
            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("TYInvoiceTab.PaidanRegisterTabID = '" + nID + "'");
            DataTable dt1 = ds.Tables[0];
            if (dt1 != null)
            {
                //if (dt1.Rows.Count > 1)
                //{
                //    MessageBox.Show("请选择不多于一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
            }
            else
            {
                MessageBox.Show("记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string outFile = "";

                   
                    string templetFile = templetFilePath + "广东省出口商品统一发票.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    ds = bllTaoDaPrinterNameTab.GetList("");
                    string strPrinterNameTemp = "";
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        strPrinterNameTemp = ds.Tables[0].Rows[0]["TaoDaPrinterName"].ToString();
                    }
   
                    try
                    {
                        eB.GDTYInvoice_DataTableToExcelAndPrint(dt, dt1, 20, 14, 3, "页", 2, strPrinterNameTemp, strBiZhiEng, this.textBoxNote.Text, this.dateTimePickerMakeDateFake.Value);
                    }
                    catch (System.Exception ex)
                    {

                    }

      

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = null;
            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet m_ds = m_bllTYInvoiceTab.ShowTYInvoiceMXRec(this.comboBoxContractNo.Text);
            string strBiZhiEng = "";
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = (DataTable)m_ds.Tables[0];
               // if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("销售单价");
                    dt.Columns.Remove("销售总额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
                string strBiZhi = m_ds.Tables[0].Rows[0]["币种"].ToString();
                DataSet dsTemp = m_bllBiZhiTab.GetList("BiZhiTab.BiZhiName = '" + strBiZhi + "'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    strBiZhiEng = dsTemp.Tables[0].Rows[0]["币制简写"].ToString();
                }
            }

            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("TYInvoiceTab.PaidanRegisterTabID = '" + nID + "'");
            DataTable dt1 = ds.Tables[0];
            if (dt1 != null)
            {
                //if (dt1.Rows.Count > 1)
                //{
                //    MessageBox.Show("请选择不多于一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
            }
            else
            {
                MessageBox.Show("记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "广东省出口商品统一发票.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.GDTYInvoice_DataTableToExcel(dt, dt1, 20, 14, 3, "页", 2, strBiZhiEng, this.textBoxNote.Text, this.dateTimePickerMakeDateFake.Value);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

             InitColumnWidth1();
        }

        private void InitColumnWidth1()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["购货单位"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["购货单位地址"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["购货单位电话"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已打印"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["打印日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["贸易方式"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收汇方式"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开户行"].Width = 200;
            ultraGrid1.DisplayLayout.Bands[0].Columns["帐号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["发运港"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["转运港"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["信用证号"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["运输工具"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["目的港"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["销货单位"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["销货单位传真"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["销货单位电话"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["销货单位地址"].Width = 200;
        }

        private void ultraGrid2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            //e.Layout.Override.DefaultRowHeight = 30;
            //e.Layout.Bands[0].Columns["是否合并"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;

            List<string> lstTemp = new List<string>();
            lstTemp.Add("数量");
            lstTemp.Add("销售总额");
            if (Frm_Main.m_bCompleteVer == true)
            {
                lstTemp.Add("报关金额");
            }

       
            ClassUltraGridCommonSet.SetSumColumn(e, lstTemp);

            InitColumnWidth2();
        }

        private void InitColumnWidth2()
        {
            ultraGrid2.DisplayLayout.Bands[0].Columns["商品名称"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["规格型号"].Width = 100;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单位"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["数量"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["销售单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["销售总额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["币种"].Width = 40;
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strTYInvoiceTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectTYInvoiceTabID = Convert.ToInt32(strTYInvoiceTabID, 10);
            cellTYInvoiceTabClick();
        }

        private void Frm_GDTYInvoice_Activated(object sender, EventArgs e)
        {
            InitContractNoCombox();
        }
        private void InitContractNoCombox()
        {
            DataSet ds = m_bllPaidanRegisterTab.GetNameAndIDAll("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }
        }
        private void Frm_GDTYInvoice_Deactivate(object sender, EventArgs e)
        {
            Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;
        }

        private void comboBoxBank_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxBank.SelectedValue.ToString();

                DataSet ds = m_bllBYCompBankCountTab.GetList("BYCompBankCountTabID = '" + str + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxAccount.Text = ds.Tables[0].Rows[0]["帐号"].ToString();
                }
            }
            catch { }
        }

        private void buttonPrintPrew_Click(object sender, EventArgs e)
        {
            DataTable dt = null;
            string str = this.comboBoxContractNo.SelectedValue.ToString();
            int nID = Convert.ToInt32(str, 10);
            DataSet m_ds = m_bllTYInvoiceTab.ShowTYInvoiceMXRec(this.comboBoxContractNo.Text);
            string strBiZhiEng = "";
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dt = (DataTable)m_ds.Tables[0];
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("销售单价");
                    dt.Columns.Remove("销售总额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
                string strBiZhi = m_ds.Tables[0].Rows[0]["币种"].ToString();
                DataSet dsTemp = m_bllBiZhiTab.GetList("BiZhiTab.BiZhiName = '" + strBiZhi + "'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    strBiZhiEng = dsTemp.Tables[0].Rows[0]["币制简写"].ToString();
                }
            }
            DataSet ds = m_bllTYInvoiceTab.ShowTYInvoiceRec("TYInvoiceTab.PaidanRegisterTabID = '" + nID + "'");
            DataTable dt1 = ds.Tables[0];
            if (dt1 != null)
            {
                //if (dt1.Rows.Count > 1)
                //{
                //    MessageBox.Show("请选择不多于一条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
            }
            else
            {
                MessageBox.Show("记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {

                    int nIsPrint = (int)dt1.Rows[0]["是否已打印"];
                    if (nIsPrint > 0)
                    {
                        if (MessageBox.Show("已经打印过，是否要重复打印？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) != DialogResult.OK)
                        {
                            return;
                        }
                    }


                    string outFile = "";


                    string templetFile = templetFilePath + "广东省出口商品统一发票.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile, true);
                    ds = bllTaoDaPrinterNameTab.GetList("");
                    string strPrinterNameTemp = "";
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        strPrinterNameTemp = ds.Tables[0].Rows[0]["TaoDaPrinterName"].ToString();
                    }

                    try
                    {
                        DGBY.Model.TYInvoiceTab modelTYInvoiceTabOld = m_bllTYInvoiceTab.GetModel(m_nSelectTYInvoiceTabID);
                        modelTYInvoiceTabOld.IsPrint = 1;
                        modelTYInvoiceTabOld.PrintDate = System.DateTime.Now.Date;
                        m_bllTYInvoiceTab.Update(modelTYInvoiceTabOld);
                        eB.GDTYInvoice_DataTableToExcelAndPrintPrew(dt, dt1, 20, 14, 3, "页", 2, strPrinterNameTemp, strBiZhiEng, this.textBoxNote.Text, this.dateTimePickerMakeDateFake.Value);
                    }
                    catch (System.Exception ex)
                    {

                    }

                 //   eB.Close();
                }
            }
            else
            {
                MessageBox.Show("明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }



    }
}